﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Vds2465Telegrams;

namespace Vds2465Parser
{
	internal static class GenParse_UserData_41_TestConfirm
	{
		#region private constants

		private const int NrOfBytes = 2;
		private const int NrOfBytesIncludingTimer = 5;

		#endregion private constants

        #region internal methods

        internal static byte[] GenerateBytes(UserData userData)
		{
			byte[] userDataBytes = null;

			if (userData is UserData_41_TestConfirm)
			{
				UserData_41_TestConfirm userData41 = userData as UserData_41_TestConfirm;
				if (userData41.TimerIncluded)
				{
					userDataBytes = new byte[NrOfBytesIncludingTimer];
					userDataBytes[0] = NrOfBytesIncludingTimer - GenParse_UserData.HeaderLength;	// user data length
					userDataBytes[1] = UserData.UserData41TestConfirm;
					userDataBytes[2] = GenParse_AddressExtension.GenerateByte_AddExt_2(userData41.AddressExtension);
					userDataBytes[3] = (byte)(userData41.MinNextTestMessage % 256);
					userDataBytes[4] = (byte)(userData41.MinNextTestMessage / 256);
				}
				else
				{
					userDataBytes = new byte[NrOfBytes];
					userDataBytes[0] = NrOfBytes - GenParse_UserData.HeaderLength;	// user data length
					userDataBytes[1] = UserData.UserData41TestConfirm;
				}
				
			}
			else
			{
				// TODO handle error
			}

			return userDataBytes;
		}

		internal static bool ParseBytes(byte[] userDataBytes, out UserData userData04TestConfirm, out int consumedBytes)
		{
			userData04TestConfirm = null;
			consumedBytes = 0;

			if (userDataBytes.Length >= NrOfBytes)
			{
				// first byte
				byte setLength = userDataBytes[0];
				if (userDataBytes.Length >= setLength + 2)
				{
					if (
						(setLength == NrOfBytes - GenParse_UserData.HeaderLength)
						|| (setLength == NrOfBytesIncludingTimer - GenParse_UserData.HeaderLength)
						)
					{
						if (userDataBytes[1] == UserData.UserData41TestConfirm)
						{
							if (setLength == NrOfBytesIncludingTimer - GenParse_UserData.HeaderLength)
							{
								eAddressExtension_2 addressExtension;
								bool bRetVal = GenParse_AddressExtension.ParseByte_AddExt_2(userDataBytes[2], out addressExtension);
								if (bRetVal)
								{
									byte lowByte = userDataBytes[3];
									byte highByte = userDataBytes[4];
									UInt16 minuteValue = (UInt16) ((highByte*256)+lowByte);
									userData04TestConfirm = new UserData_41_TestConfirm(addressExtension, minuteValue);
									consumedBytes = NrOfBytesIncludingTimer;
									return true;
								}

								// TODO handle error: wrong value address extension
								return false;
							}
							else
							{
								userData04TestConfirm = new UserData_41_TestConfirm();
								consumedBytes = NrOfBytes;
								return true;
							}
						}

						// TODO handle error: wrong value for the data type
						return false;
					}

					// TODO handle error (wrong value in first byte the length)
					return false;
				}
				// TODO handle error: not enough bytes as specified by the SL value
				return false;
			}
			// TODO handle error (not enough bytes)
			return false;
        }

        #endregion internal methods
    }
}
